package com.example.train_operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.train_operate.dto.TaskDTO;
import com.example.train_operate.dto.UserDTO;
import com.example.train_operate.entity.Experiment;
import com.example.train_operate.service.ExperimentService;
import com.example.train_operate.mapper.ExperimentMapper;
import com.example.train_operate.util.Result;
import com.example.train_operate.util.UserHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.example.train_operate.util.FileUrlConstants.FILE_URL;

/**
* @author 21609
* @description 针对表【tb_experiment】的数据库操作Service实现
* @createDate 2025-08-13 17:06:39
*/
@Service
public class ExperimentServiceImpl extends ServiceImpl<ExperimentMapper, Experiment>
    implements ExperimentService{

    @Resource
    private ExperimentMapper experimentMapper;

    /**
     * 添加系统实验任务
     * @param experiment
     * @return
     */
    @Override
    public Result addExperiment(Experiment experiment) {
        try{
            UserDTO user = UserHolder.getUser();
            Integer userId = user.getId();
            experiment.setUserId(userId);
            String photoUrl = experiment.getPhotoUrl();
            if(photoUrl != null && !photoUrl.isEmpty()){
                String substring = photoUrl.substring(FILE_URL.length());
                experiment.setPhotoUrl(substring);
            }
            experimentMapper.insert(experiment);
            QueryWrapper<Experiment> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId)
                    .orderByDesc("create_time")
                    .last("limit 1");
            Experiment experiment1 = experimentMapper.selectOne(queryWrapper);
            return Result.ok(experiment1).message("系统实验已完成");
        }catch (Exception e){
            return Result.fail().message("出错了");
        }
    }

    /**
     * 根据id查询系统实验任务
     * @param experimentId
     * @return
     */
    @Override
    public Result getExperimentById(Integer experimentId) {
        try{
            QueryWrapper<Experiment> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("experiment_id", experimentId)
                    .eq("is_delete", 0);
            Experiment experiment = experimentMapper.selectOne(queryWrapper);
            if (experiment == null){
                return Result.fail().message("没有此条系统实验检修信息");
            }
            String photoUrl = experiment.getPhotoUrl();
            if(photoUrl != null && !photoUrl.isEmpty()){
                String url = FILE_URL + photoUrl;
                experiment.setPhotoUrl(url);
            }
            return Result.ok(experiment);
        }catch (Exception e){
            return Result.fail();
        }
    }

    /**
     * 修改系统实验任务
     * @param experiment
     * @return
     */
    @Override
    public Result updateExperiment(Experiment experiment) {
        try{
            String photoUrl = experiment.getPhotoUrl();
            if(photoUrl != null || !photoUrl.isEmpty()){
                String substring = photoUrl.substring(FILE_URL.length());
                experiment.setPhotoUrl(substring);
            }
            experimentMapper.updateById(experiment);
            Experiment experiment1 = experimentMapper.selectById(experiment.getExperimentId());
            return Result.ok(experiment1).message("修改成功");
        }catch (Exception e){
            return Result.fail().message("修改失败");
        }
    }
}




